
package com.pp.admin.main.client.tab;

import java.util.Iterator;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.pp.admin.common.client.beans.BaseBean;
import com.pp.admin.common.client.beans.BaseUser;
import com.pp.admin.common.client.beans.ListContents;
import com.pp.admin.common.client.beans.PageInfo;
import com.pp.admin.common.client.helper.AdminFlexTable;
import com.pp.admin.main.client.common.BaseContentsPanel;
import com.pp.admin.main.client.common.CrudOp;
import com.pp.admin.main.client.common.Pagination;
import com.pp.admin.main.client.common.TabListContents;

/**
 * @author pandurang
 */
public class DomainUserPage extends AdminBasePage {
	
	private static int	TAB_CONTENTS_INDEX	= 1;
	
	public DomainUserPage( BaseTab parent ) {

		super();
		this.parentTab = parent;
		setTabContents( new ListDomainUserPanel( this ) );
	}
	
	private void setTabContents( VerticalPanel contents ) {

		this.tabContents = contents;
		cleanContents();
		this.add( contents );
	}
	
	private void cleanContents() {

		/**
		 * not removing the 1st widget as the common widget added by AdminBasePage should not be
		 * cleared
		 */
		if ( getWidgetCount() > getCommonWidgetCount() ) {
			remove( TAB_CONTENTS_INDEX );
		}
	}
	
	@Override
	public void refresh() {

		if ( !( this.tabContents instanceof TabListContents ) ) {
			setTabContents( new ListDomainUserPanel( this ) );
		}
		( (TabListContents) this.tabContents ).refresh();
	}
	
	/*
	 * (non-Javadoc)
	 * @seecom.google.gwt.event.dom.client.ChangeHandler#onChange(com.google.gwt.event.dom.client.
	 * ChangeEvent)
	 */
	@Override
	public void onChange( ChangeEvent event ) {

		refresh();
	}
}

/**
 * @author pandurang
 */
class ListDomainUserPanel extends BaseContentsPanel implements TabListContents {
	
	static final int		LIST_HEADER_ROW			= 0;
	
	static final int		USER_NAME_COL			= 0;
	
	static final int		FIRST_NAME_COL			= 1;
	
	static final int		LAST_NAME_COL			= 2;
	
	static final int		STATUS_COL				= 3;
	
	private CrudAddUserOp	crudOperationsTop		= new CrudAddUserOp();
	
	private CrudAddUserOp	crudOperationsBottom	= new CrudAddUserOp();
	
	private Pagination		pagination				= null;
	
	private AdminFlexTable	contents				= new AdminFlexTable();
	
	ListDomainUserPanel( DomainUserPage parent ) {

		pagination = new Pagination( this, new PageInfo() );
		this.parent = parent;
		this.add( crudOperationsTop );
		contents.setWidget( LIST_HEADER_ROW, USER_NAME_COL, new HTML( myConstants.username() ) );
		contents.setWidget( LIST_HEADER_ROW, FIRST_NAME_COL, new HTML( myConstants.firstName() ) );
		contents.setWidget( LIST_HEADER_ROW, LAST_NAME_COL, new HTML( myConstants.lastName() ) );
		contents.setWidget( LIST_HEADER_ROW, STATUS_COL, new HTML( myConstants.status() ) );
		this.add( contents );
		this.add( pagination );
		this.add( crudOperationsBottom );
	}
	
	private void list() {

		BasePage.userService.listBaseUsersByDomainID( ( (DomainUserPage) this.parent ).getSelectedDomainID(), pagination.getPage(), new AsyncCallback<ListContents>() {
			
			@Override
			public void onSuccess( ListContents result ) {

				pagination.refresh( result.getPageInfo() );
				refresh( result );
			}
			
			@Override
			public void onFailure( Throwable caught ) {

				// TODO Auto-generated method stub
				
			}
		} );
	}
	
	private void refresh( ListContents listContents ) {

		Iterator<BaseBean> itrBaseUsers = listContents.getContents().iterator();
		BaseUser baseUser = null;
		if ( contents.getRowCount() > 1 ) {
			int rowCount = contents.getRowCount();
			for ( int index = rowCount - 1; index > 0; index-- ) {
				contents.removeRow( index );
			}
		}
		int rowCount = 1;
		while ( itrBaseUsers.hasNext() ) {
			baseUser = (BaseUser) itrBaseUsers.next();
			contents.setWidget( rowCount, USER_NAME_COL, new HTML( baseUser.getUserID() ) );
			contents.setWidget( rowCount, FIRST_NAME_COL, new HTML( baseUser.getFirstName() ) );
			contents.setWidget( rowCount, LAST_NAME_COL, new HTML( baseUser.getLastName() ) );
			String status = null;
			if ( baseUser.getStatus() == BaseUser.STATUS_ACTIVE ) {
				status = myConstants.active();
			} else if ( baseUser.getStatus() == BaseUser.STATUS_DELETED ) {
				status = myConstants.disabled();
			} else if ( baseUser.getStatus() == BaseUser.STATUS_REQUESTED ) {
				status = myConstants.requsted();
			}
			contents.setWidget( rowCount, STATUS_COL, new HTML( status ) );
			rowCount++;
		}
		
	}
	
	@Override
	public void refresh() {

		list();
	}
	
}

class CrudAddUserOp extends CrudOp {
	
	private Button	add	= new Button( myConstants.adduser() );
	
	public CrudAddUserOp() {

		add( add );
	}
	
	public void addAddUserClickHandler( ClickHandler handler ) {

		add.addClickHandler( handler );
	}
}
